<?php

namespace app\controller;

use app\BaseController;
use app\model\SysClass;
use app\model\SysDictionary;
use app\model\SysDictionaryValue;
use app\utils\CamlHump;
use app\utils\ResposeUtils;
use think\facade\Request;

class SysDictionayController  extends BaseController
{


    public function list()
    {
        $body = Request::post();
        $query = SysDictionary::createQuery($body);
        return ResposeUtils::ok(
            $query->select(),
            200,
            $query->count()
        );
    }


    function getbyid()
    {
        
        $id = Request::get("id");
        $one = SysDictionary::findOne($id);

        //逻辑字段 className
        if(isset($one['classId'])){
            if($one['classId']>0){
                $classOne=SysClass::findOne($one['classId']);
                $one['className']=$classOne['name']??'';
            }
        }
        return ResposeUtils::ok(
            $one
        );
    }
    function update()
    {
        $body = Request::post();
        $updateersuilt=SysDictionary::updatePlus($body,$body['id']);
        return ResposeUtils::ok(
            $updateersuilt['id']
        );
    }
    function add()
    {
        $body = Request::post();
        $result=SysDictionary::addPlus($body);
        return ResposeUtils::ok(
            $result['id']
        );
    }
    function deletebyid()
    {
        $id = Request::get('id');
        $result=SysDictionary::destroy($id);
        return ResposeUtils::ok(
            $result
        );
    }

    /*==================value*/

    public function valuelist()
    {
        $body = Request::post();
        $query = SysDictionaryValue::createQuery($body);
        return ResposeUtils::ok(
            $query->select(),
            200,
            $query->count()
        );
    }


    function valuegetbyid()
    {

        $id = Request::get("id");
        $one = SysDictionaryValue::findOne($id);

        //逻辑字段 className
        if(isset($one['classId'])){
            if($one['classId']>0){
                $classOne=SysClass::findOne($one['classId']);
                $one['className']=$classOne['name']??'';
            }
        }
        return ResposeUtils::ok(
            $one
        );
    }
    function valueupdate()
    {
        $body = Request::post();
        $updateersuilt=SysDictionaryValue::updatePlus($body,$body['id']);
        return ResposeUtils::ok(
            $updateersuilt['id']
        );
    }
    function valueadd()
    {
        $body = Request::post();
        $result=SysDictionaryValue::addPlus($body);
        return ResposeUtils::ok(
            $result['id']
        );
    }
    function valuedeletebyid()
    {
        $id = Request::get('id');
        $result=SysDictionaryValue::deletePlus($id);
        return ResposeUtils::ok(
            $result
        );
    }

    function listbycode(){
        $code = Request::post('code');
        if(!isset($code)){
            return ResposeUtils::ok(
                []
            );
        }
        //先找到 dic
        $dicOne=SysDictionary::findOneByCode($code);
        if(!isset($dicOne)){
            return ResposeUtils::ok(
                []
            );
        }

        $query1 = SysDictionaryValue::createQuery([
            "dicId" => $dicOne['id']
        ]);
        $dicvaluelist = CamlHump::toCamlArray($query1->select());



        return ResposeUtils::ok(
                   $dicvaluelist
               );
    }
}
